/**
 * Copyright (c) 2017 hadlinks, All Rights Reserved.
 */
package com.bugull.farm.core.proxy.example;

import com.alibaba.fastjson.JSONObject;
import com.bugull.farm.core.annotation.LogAnnotation;
import com.bugull.farm.core.proxy.BeforeHandler;
import com.bugull.farm.core.utils.StringUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.reflect.Method;

/**
 * Project Name: bugu-farm-household 
 * Package Name: com.bugull.farm.core.proxy.example 
 * ClassName: BeforeHandlerImpl 
 * Function: TODO ADD FUNCTION.  
 * date: 2017/11/7 17:15
 * @author songwei (songw@hadlinks.com)
 * @since JDK 1.8 
 */
public class BeforeHandlerImpl extends BeforeHandler {

    private Logger logger = LogManager.getLogger(BeforeHandlerImpl.class);

    @Override
    public void handleBefore(Object proxy, Method method, Object[] args) {
        //监控日志注解，带有LogAnnotation的方法，自动添加日志记录
        if (method.isAnnotationPresent(LogAnnotation.class)) {
            JSONObject message = null;
            for (Object arg : args) {
                if (arg instanceof JSONObject) {
                    message = (JSONObject)arg;
                }
            }
            //注解中的附加信息
            String appender = method.getAnnotation(LogAnnotation.class).value();
            Logger logger = LogManager.getLogger(getTargetObject().getClass());
            logger.debug("{}(注解：{})处理数据{}", getTargetObject().getClass()
                    , appender
                    , StringUtil.printJson(message));
        }
    }
}
